﻿using System;
using System.Collections.Generic;
using System.Text;
using dtSearch.Engine;

namespace DTSearchHelper
{
    public class SearchStatusHander:ISearchStatusHandler
    {
        public delegate void ItemFoundDelegate(object Sender, ItemFoundEventArgs e);
        public event ItemFoundDelegate ItemFound;

        private int indexCount = 0;
        private int totalCount = 0;

        private AbortValue abortValue = AbortValue.Continue;
        public AbortValue AbortValue
        {
            get { return abortValue; }
            set { abortValue = value; }
        }

        private string filename = "";
        public string FileName
        {
            get { return filename; }
        }

        private string index = "";
        public string Index
        {
            get { return index; }
        }

        #region ISearchStatusHandler Members

        public AbortValue CheckForAbort()
        {
            return abortValue;
        }

        public void OnFound(SearchResultsItem item)
        {
            this.indexCount++;
            this.totalCount++;

            ItemFoundEventArgs e = new ItemFoundEventArgs(item,indexCount,totalCount);
            if (this.ItemFound != null)
            {
                this.ItemFound(this, e);
            }
            item.VetoThisItem = e.KeepItemInSearchResults;
        }

        public void OnSearchingFile(string filename)
        {
            this.filename = filename;
        }

        public void OnSearchingIndex(string index)
        {
            this.indexCount = 0;
            this.index = index;
        }

        #endregion
    }

    public class ItemFoundEventArgs:EventArgs
    {
        private SearchResultsItem myItem = null;
        public SearchResultsItem MyItem
        {
            get { return myItem; }
        }


        private int indexCount = 0;
        public int IndexCount
        {
            get { return indexCount; }
        }
        
        private int totalCount = 0;
        public int TotalCount
        {
            get { return totalCount; }
            set { totalCount = value; }
        }
        
        private bool keepItemInSearchResults = false;
        public bool KeepItemInSearchResults
        {
            get { return keepItemInSearchResults; }
            set { keepItemInSearchResults = value; }
        }


        
        public ItemFoundEventArgs(SearchResultsItem item,int indexCount,int totalCount)
        {
            this.myItem=item;
            this.indexCount = indexCount;
            this.totalCount = totalCount;
        }
    }

}
